package com.cogent.dynamicform.form.impl;

import java.util.HashMap;

import com.cogent.common.util.MapUtil;
import com.cogent.dynamicform.domain.BaseFormColumnDomain;
import com.cogent.dynamicform.domain.FormColumnDomain;
import com.cogent.dynamicform.form.AbstractDynamicForm;
import com.cogent.dynamicform.util.DynamicFormBusUtil;
import com.opensymphony.oscache.util.StringUtil;

public class FileUploadDynamicFormImpl extends AbstractDynamicForm {
	public String genQueryField(BaseFormColumnDomain formColumnDomain, HashMap formMap) {
		return "";
	}
	
	public String genInputField(FormColumnDomain formColumnDomain, HashMap formMap) {
		if(formColumnDomain.getViewmodel() > 0)
			return getShowValue(formColumnDomain, formMap);
		String[] typecodes = formColumnDomain.getTypecode().split("~");		
		StringBuffer str = new StringBuffer();
		str.append("<div id='fileId' style='height:40'></div>");
		str.append("<input type='file' id='hdnFileId' name='upload'/>");
		if(!MapUtil.isEmpty(formMap, typecodes[1])) {
			str.append("<div id='existfile' style='height:30px;'>").append("<div style='width: 70%;float: left;'><a target=\"_blank\" href=\"").append(MapUtil.getStringValue(formMap, typecodes[0])) 
				.append("\">").append(MapUtil.getStringValue(formMap, typecodes[1])).append("&nbsp;</a><input name=\"cur_file_name\" type=\"hidden\" value=\"").append(MapUtil.getStringValue(formMap, typecodes[0]))
				.append("\" /></div>").append("  <img id='clearexistfile0' src='images/btn_clear.gif' alt='清空' width='22' height='22' style='cursor: hand;' onclick='javascript:delExistFile(\"existfile\");'/>")
				.append("</div>");
		}
	    
	    return str.toString();
	}
	
	public String getShowValue(BaseFormColumnDomain formColumnDomain, HashMap formMap) {
		String[] typecodes = formColumnDomain.getTypecode().split("~");		
		StringBuffer str = new StringBuffer();
		str.append("<a target=\"_blank\" href=\"").append(MapUtil.getStringValue(formMap, typecodes[0])).append("\" >")
			.append(MapUtil.getStringValue(formMap, typecodes[1])).append("&nbsp;</a>");
		
		return str.toString();
	}
	
	public void dealFormValue(FormColumnDomain formColumnDomain, HashMap formMap) {
		String[] typecodes = formColumnDomain.getTypecode().split("~");
		String curFile = MapUtil.getStringValue(formMap, "cur_file_name");
		if(!MapUtil.isEmpty(formMap, DynamicFormBusUtil.DYNAMICFORM_FILPATH)) {
			curFile = MapUtil.getStringValue(formMap, DynamicFormBusUtil.DYNAMICFORM_FILPATH);
			String curFileName = MapUtil.getStringValue(formMap, DynamicFormBusUtil.DYNAMICFORM_REALFILENAME);
			formMap.put(typecodes[1], curFileName);
		} else if(StringUtil.isEmpty(curFile))
			formMap.put(typecodes[1], "");
		formMap.put(typecodes[0], curFile);
	}
}
